﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Crossroad.Core.Models;
using Crossroad.Utils.Extensions;
using Crossroad.Utils.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace Crossroad.Core.Attributes
{
    public class ApiResultFilterAttribute : Attribute, IActionFilter
    {
        /// <summary>
        /// 执行后
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuted(ActionExecutedContext context)
        {
            if (context.Exception != null)
                return;

            var res = new ResDataPagination
            {
                code = 200,
                message = "操作成功"
            };

            //返回的结果
            var content = context.Result as ObjectResult;

            //无返回值
            if(content.IsNull() || content.Value == null)
                return;

            //原样返回
            if (content.Value.GetType().FullName.Contains(typeof(ResDicData).FullName))
            {
                var dicObj = content.Value as ResDicData;
                content.Value = dicObj.Result;
                return;
            }

            //权限注入
            var permission = context.ActionDescriptor.FilterDescriptors.AsEnumerable()
                .FirstOrDefault(p => p.Filter.GetType().Name.Equals("PermissionAttribute"));
            if (permission.NotNull())
            {
                var value = permission.Filter.GetMemberValue("Options") as List<string>;
                if(value.NotNull())
                    res.permissions = value;
            }

            //有分页参数
            if (content.Value.GetType().FullName.Contains(typeof(Pagination<>).FullName))
            {
                var type = content.Value.GetType();
                foreach (var item in type.GetProperties())
                {
                    var attrName = item.GetMethod.Name.Replace("get_", "");
                    var attrValue = item.GetValue(content.Value);
                    switch (attrName)
                    {
                        case "PageIndex":
                            res.page_index = (int)attrValue;
                            break;
                        case "PageSize":
                            res.page_size = (int)attrValue;
                            break;
                        case "TotalCount":
                            res.total_count = (int)attrValue;
                            break;
                        case "Records":
                            res.data = attrValue;
                            break;
                    }
                }

                content.Value = res;
            }
            //无分页参数
            else
            {
                res.data = content.Value;
                ResData rd = new ResData();
                res.CopyTo(rd);
                content.Value = rd;
            }

        }

        /// <summary>
        /// 执行前
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuting(ActionExecutingContext context)
        {
        }


    }
}
